<!--  -->
<template>
  <div class="login-container">
    <el-form
      :model="user"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item prop="mobile">
        <el-input type="text" placeholder="请输入手机号" v-model="user.mobile"></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input type="password" placeholder="请输入密码" v-model="user.code" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item prop="agree">
        <el-checkbox v-model="user.agree">我已阅读用同意用户协议和隐私条款</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="login" @click="itmlogin()" :loading="isloading">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getLogin } from "@/api/login";
export default {
  data() {
    return {
      user: {
        mobile: "13911111111",
        code: "246810",
        agree: false
      },
      rules: {
        mobile: [
          { required: true, message: "请输入手机号", trigger: "change" }
        ],
        code: [{ required: true, message: "请输入密码", trigger: "change" }],
        agree: [
          {
            validator: (rule, value, callback) => {
              if (this.user.agree) {
                callback();
                
                
              } else {
                callback(new Error("请同意用户协议"));
                
              }
            },
            trigger: "blur"
          }
        ]
      },
      isloading: false
    };
  },
  methods: {
    itmlogin() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          this.login();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    login() {
      this.isloading = true;
      getLogin(this.user)
        .then(res => {
          this.isloading = false;
          this.$message({
            message: "登录成功",
            type: "success"
          });
          window.sessionStorage.setItem('user',JSON.stringify(res.data.data))
          
          this.$router.push('/')
        })
        .catch(err => {
          console.log(err);
          if(err===' Network Error'){
             this.$message.error("网络错误");
          }
          else{
          this.$message.error("请确保正确输入账号和密码");
          }
          
           this.isloading = false;
        });
    }
  }
};
</script>
<style scoped>
.login-container {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.el-form {
  width: 400px;
}
.login {
  width: 100%;
}
</style>